package labuladong.BFS.二叉树最小深度;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;

public class 二叉树最小深度 {
    public static void main(String[] args) {

    }

    public static int minDepth(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        Set<TreeNode> set = new HashSet<>();
        if (root == null) return 0;
        queue.offer(root);
        set.add(root);
        int depth = 1;
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i=0; i<size; i++) {
                TreeNode cur = queue.poll();
                if (cur.left==null && cur.right==null) {
                    return depth;
                }
                if (cur.left != null)  queue.offer(cur.left);
                if (cur.right != null)  queue.offer(cur.right);
            }
            depth++;
        }
        return depth;
    }
}
